/*
 * GmOptions.java
 *
 * Created on April 10, 2007, 6:02 PM
 */

package d20chat;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;       // Used for ActionEvents
import java.awt.event.*;
import java.awt.*;
import java.lang.String;
import javax.swing.JOptionPane;


/**
 * Displays the GM options window.
 * Used for setting or clearing the password, as well as toggling if character
 * sheets are visible.
 */
public class GmOptions extends javax.swing.JFrame {
    
    /**
     * Current instance of D20Chat, used to access variables such as password.
     */
    private D20Chat tempMain;
    private String temppwd1;
    private String temppwd2;
    private JFrame frame = new JFrame();
    
    /** Creates new form GmOptions */
/**
     * Class constructor for GmOptions.
     * @param temp2Main Current instance of D20Chat, used to access variables such as password.
     */
    public GmOptions(D20Chat temp2Main) {
        initComponents();
        
        // Center GmOptions jFrame
        Dimension scrn = getToolkit().getScreenSize();
        this.setBounds( (scrn.width-getWidth())/2, (scrn.height-getHeight())/2, getWidth(), getHeight() );
        
        tempMain = temp2Main;
        
        //System.out.println("password "+tempMain.serverPassword);
        
        if (tempMain.serverPassword != null)
            ctpwdjTextField.setText(tempMain.serverPassword);
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
/**
     * 
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        canceljButton = new javax.swing.JButton();
        savejButton = new javax.swing.JButton();
        gmOptionslabel = new javax.swing.JLabel();
        rmpwdjLabel = new javax.swing.JLabel();
        cmpwdjLabel = new javax.swing.JLabel();
        charSheetjCheckBox = new javax.swing.JCheckBox();
        jSeparator1 = new javax.swing.JSeparator();
        rmpwdjTextField = new javax.swing.JTextField();
        cmpwdjTextField = new javax.swing.JTextField();
        ctpwdjTextField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        removePasswordCheckBox = new javax.swing.JCheckBox();

        setTitle("GM Options");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        canceljButton.setText("Cancel");
        canceljButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                canceljButtonActionPerformed(evt);
            }
        });

        savejButton.setText("Save");
        savejButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                savejButtonActionPerformed(evt);
            }
        });

        gmOptionslabel.setFont(new java.awt.Font("Tahoma", 1, 14));
        gmOptionslabel.setText("GM Options:");

        rmpwdjLabel.setText("New Password:");

        cmpwdjLabel.setText("Confirm Password:");

        charSheetjCheckBox.setSelected(true);
        charSheetjCheckBox.setText("Allow players to view character sheets");
        charSheetjCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        charSheetjCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));

        ctpwdjTextField.setEditable(false);

        jLabel1.setText("Current Password:");

        removePasswordCheckBox.setText("Remove the password");
        removePasswordCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        removePasswordCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(savejButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 152, Short.MAX_VALUE)
                        .addComponent(canceljButton))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addComponent(gmOptionslabel))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ctpwdjTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(rmpwdjLabel))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(cmpwdjLabel)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cmpwdjTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                            .addComponent(rmpwdjTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE))
                .addGap(20, 20, 20))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(removePasswordCheckBox)
                .addContainerGap(159, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(charSheetjCheckBox)
                .addContainerGap(81, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(gmOptionslabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(ctpwdjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(removePasswordCheckBox)
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rmpwdjLabel)
                    .addComponent(rmpwdjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmpwdjLabel)
                    .addComponent(cmpwdjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(charSheetjCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(savejButton)
                    .addComponent(canceljButton))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        this.charSheetjCheckBox.setSelected(tempMain.charsheet);
    }//GEN-LAST:event_formWindowOpened

    
/********************************************************************************
  Method:
  Description:
  Parameters:
  Used by:
  Uses:
********************************************************************************/
    private void canceljButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_canceljButtonActionPerformed
        // Hide form
        this.setVisible(false);
    }//GEN-LAST:event_canceljButtonActionPerformed

    
/********************************************************************************
  Method:
  Description:
  Parameters:
  Used by:
  Uses:
********************************************************************************/
    private void savejButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_savejButtonActionPerformed
        String info;
        temppwd1 = rmpwdjTextField.getText().toString();
        temppwd2 = cmpwdjTextField.getText().toString();
        //.out.println("temppwd2 "+temppwd2+"  =  "+temppwd1);

        if( charSheetjCheckBox.isSelected() )
        {
                info = "/charactersheet on";
                tempMain.OutParse.sendMessage( info );
        }
        else
        {
                info = "/charactersheet off";
                tempMain.OutParse.sendMessage( info );
        }

        if( removePasswordCheckBox.isSelected() == true )
        {
            info = "/setPassword none";
            tempMain.OutParse.sendMessage( info );
            this.setVisible(false);
            tempMain.serverPassword = null;
            
        }
        else if (!temppwd1.equals(temppwd2))
        {
            // Let user know that passwords do not match
            JOptionPane.showMessageDialog(frame, "Passwords do not match. Please try again.", "GM Options", JOptionPane.ERROR_MESSAGE);

            // reset both password fields, temppwd1 and temppwd2
            temppwd1 = null;
            temppwd2 = null;
            rmpwdjTextField.setText("");
            cmpwdjTextField.setText("");
        }
        else
        {
            tempMain.serverPassword = temppwd1;
            
            if( !tempMain.serverPassword.isEmpty() ) 
            {
                info = "/setPassword " + tempMain.serverPassword;
                tempMain.OutParse.sendMessage( info );
            }
            //tempMain.OutParse.sendMessage(""+temppwd1);
            
            // Hide form
            this.setVisible(false);
        }
        
    }//GEN-LAST:event_savejButtonActionPerformed
       
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton canceljButton;
    private javax.swing.JCheckBox charSheetjCheckBox;
    private javax.swing.JLabel cmpwdjLabel;
    private javax.swing.JTextField cmpwdjTextField;
    private javax.swing.JTextField ctpwdjTextField;
    private javax.swing.JLabel gmOptionslabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JCheckBox removePasswordCheckBox;
    private javax.swing.JLabel rmpwdjLabel;
    private javax.swing.JTextField rmpwdjTextField;
    private javax.swing.JButton savejButton;
    // End of variables declaration//GEN-END:variables
    
}
